package com.kmust.dream.wanderingdream.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmust.dream.wanderingdream.comm.pojo.ResultBean;
import com.kmust.dream.wanderingdream.product.entity.ProductSku;
import com.kmust.dream.wanderingdream.product.service.ProductSkuService;
import com.kmust.dream.wanderingdream.product.mapper.ProductSkuMapper;
import com.kmust.dream.wanderingdream.product.utils.ParseUtils;
import com.kmust.dream.wanderingdream.product.utils.ReturnUtils;
import com.kmust.dream.wanderingdream.product.vo.GetSkuVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 *
 */
@Service
@Transactional
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku>
        implements ProductSkuService {

    @Override
    public ResultBean getAllSku() {
        return ReturnUtils.getList(list());
    }

    @Override
    public ResultBean addSku(ProductSku productSku) {
        ParseUtils.updateSku(productSku);
        return ReturnUtils.add(save(productSku));
    }

    @Override
    public ResultBean updateSku(ProductSku productSku) {
        System.out.println(productSku);
        ParseUtils.updateSku(productSku);
        return ReturnUtils.update(this.updateById(productSku));
    }

    @Override
    public ResultBean deleteSku(Integer skuId) {
        UpdateWrapper<ProductSku> productSkuUpdateWrapper = new UpdateWrapper<>();
        productSkuUpdateWrapper.eq("product_sku_id", skuId).set("status", false);
        return ReturnUtils.delete(update(productSkuUpdateWrapper));
    }

    @Override
    public ResultBean getSkuVersion(Integer skuId) {
        return ReturnUtils.getOne(this.getOne(new QueryWrapper<ProductSku>().eq("product_sku_id", skuId).select("version")));
    }

    @Override
    public ResultBean updateVersionAndStock(ProductSku productSku, Integer orderNumber) {
        UpdateWrapper<ProductSku> productSkuWrapper = new UpdateWrapper<>();
        productSkuWrapper.eq("product_sku_id", productSku.getProductSkuId());
        productSkuWrapper.set("version", productSku.getVersion() + 1);
        productSkuWrapper.set("product_sku_stock", productSku.getProductSkuStock() - orderNumber);
        return ReturnUtils.update(this.update(productSku, productSkuWrapper));
    }

    @Override
    public ResultBean getSku(GetSkuVO getSkuVO) {
        String s = ParseUtils.reChange(ParseUtils.changeSort(getSkuVO.getProperty()));
        getSkuVO.setProperty(s);
        QueryWrapper<ProductSku> eq = new QueryWrapper<ProductSku>()
                .eq("product_id", getSkuVO.getProductId())
                .eq("product_sku_properties", getSkuVO.getProperty());
        return ReturnUtils.getOne(getOne(eq));
    }

    @Override
    public ResultBean getSkuById(Integer skuId) {
        return ReturnUtils.getOne(getById(skuId));
    }

    @Override
    public ResultBean getSkuStock(Integer skuId) {
        ProductSku byId = getById(skuId);
        Integer productSkuStock = byId.getProductSkuStock();
        return ReturnUtils.getOne(productSkuStock);
    }

    @Override
    public ResultBean getAllSkuByProductId(Integer productId) {
        QueryWrapper<ProductSku> condition = new QueryWrapper<ProductSku>()
                .eq("product_id", productId);
        List<ProductSku> list = list(condition);
        return ReturnUtils.getList(list);
    }
}




